package com.jkgh.remedium.rsgen.generator.java;

import java.util.HashMap;
import java.util.Map;

public class RemediumProducerJavaGenerator {

	private static interface Handler {

		public String handle(String message, String name);
	}

	private final static Handler OBJECT_HANDLER = new Handler() {

		@Override
		public String handle(String message, String name) {

			return message + ".produceObject(" + name + ", consumer)";
		}
	};

	private final static Handler OBJECT_ARRAY_HANDLER = new Handler() {

		@Override
		public String handle(String message, String name) {

			return "RemediumTools.produceObjectArray(consumer, " + message + ", " + name + ")";
		}
	};

	private static final Map<String, Handler> HANDLERS = new HashMap<String, Handler>();

	static {

		HANDLERS.put("String", new Handler() {

			@Override
			public String handle(String message, String name) {

				return "RemediumTools.produceString(consumer, " + name + ")";
			}
		});

		HANDLERS.put("String[]", new Handler() {

			@Override
			public String handle(String message, String name) {

				return "RemediumTools.produceStringArray(consumer, " + name + ")";
			}
		});

		HANDLERS.put("int", new Handler() {

			@Override
			public String handle(String message, String name) {

				return "consumer.consumeInt(" + name + ");";
			}

		});

		HANDLERS.put("int[]", new Handler() {

			@Override
			public String handle(String message, String name) {

				return "RemediumTools.produceIntArray(consumer, " + name + ")";
			}

		});

		HANDLERS.put("double", new Handler() {

			@Override
			public String handle(String message, String name) {

				return "consumer.consumeDouble(" + name + ")";
			}

		});

		HANDLERS.put("double[]", new Handler() {

			@Override
			public String handle(String message, String name) {

				return "RemediumTools.produceDoubleArray(consumer, " + name + ")";
			}

		});

		HANDLERS.put("byte", new Handler() {

			@Override
			public String handle(String message, String name) {

				return "consumer.consumeInt(" + name + ")";
			}

		});

		HANDLERS.put("byte[]", new Handler() {

			@Override
			public String handle(String message, String name) {

				return "RemediumTools.produceByteArray(consumer, " + name + ")";
			}

		});
	}

	public static String generate(String className, String name, String message) {

		Handler handler = HANDLERS.get(className);

		if (handler == null) {
			if (className.endsWith("[]")) {
				handler = OBJECT_ARRAY_HANDLER;
			} else {
				handler = OBJECT_HANDLER;
			}
		}

		String ret = handler.handle(message, name);

		return ret;
	}

}
